/*
 * SecureFile.h
 *
 *  Created on: Aug 29, 2011
 *      Author: Gherman Alin
 */

#ifndef SECUREFILE_H_
#define SECUREFILE_H_

#include "SecureContainer.h"
#include <map>
#include <istream>
#include <ostream>
#include <boost/thread/mutex.hpp>

namespace DataAccess {

class SecureFile {
	friend class boost::serialization::access;
	template<class Archive>
	void serialize(Archive & ar, const unsigned int version) {
		ar & BOOST_SERIALIZATION_NVP(containers);
		std::cout << "serialized SecureFile:" << containers.size() << std::endl;
	}

public:
	SecureFile();
	SecureFile(const std::string& fileName);
	virtual ~SecureFile();

	bool setContainer(const std::string& name, const std::string& pass);
	bool removeContainer(const std::string& name);

	bool containerExists(const std::string& container);

	std::vector<std::string> getContainersNames();
	std::vector<std::string> getKeys(const std::string& container);

	bool set(std::string& key, const std::string& container, const std::string& value);
	bool set(std::string& key, const std::string& container, const std::vector<char>& data);
	bool set(std::string& key, const std::string& container, std::istream& data);

	bool getStr(std::string& key, const std::string& container, std::string& out);
	bool getData(std::string& key, const std::string& container, std::vector<char>& out);
	bool getInOutstream(const std::string& key, const std::string& container, std::ostream& out);

	bool remove(const std::string& key, const std::string& container);

	bool load(const std::string& filePath);
	bool save(const std::string& filePath);

private:
	SecureContainer* getContainer(const std::string& name);
	SecureContainer* getOrCreateContainer(const std::string& name);

	std::string filePath;
	std::map<std::string, SecureContainer> containers;
	boost::mutex thisMutex;
};

}

#endif /* SECUREFILE_H_ */
